package com.lpfy.dtxc.viewutils;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.MotionEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * @author: hanGuangHui
 * @data: 2024/1/18
 * @description:
 */
public class PdfView extends WebView {

    public boolean isTop = true, isBottom = false;//用于判断滑动位置

    private final static String PDFJS = "file:///android_asset/pdfload/index.html?";

    public PdfView(Context context) {
        super(context);
        init();
    }

    public PdfView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }

    private void init() {
        WebSettings settings = getSettings();
        //设置自适应屏幕，两者合用
        settings.setUseWideViewPort(true);  //将图片调整到适合webview的大小
        settings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        settings.setJavaScriptEnabled(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(false);
        settings.setAllowUniversalAccessFromFileURLs(true);

        setWebViewClient(new WebViewClient() {// 注册滑动监听方法viewerContainer为加载pdf的viewid
            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                //滑动监听
                String startSave = "\n" +
                        "document.getElementById(\"viewerContainer\").addEventListener('scroll',function () {\n" +
                        "if(this.scrollHeight-this.scrollTop - this.clientHeight < 50){\n" +
                        "window.java.bottom(); \n" +
                        "}\n" +
                        "else if(this.scrollTop==0){\n" +
                        "window.java.top(); \n" +
                        "}\n" +
                        "else {\n" +
                        "window.java.scrolling(); \n" +
                        "}\n" +
                        "});";
                webView.loadUrl("javascript:" + startSave);
            }
        });
        addJavascriptInterface(new Object() {
            @JavascriptInterface
            public void bottom() {
                Log.e("msg+++++++", "到了低端");
                isBottom = true;
                isTop = false;
            }

            @JavascriptInterface
            public void scrolling() {
                Log.e("msg+++++++", "滑动中");
                isBottom = false;
                isTop = false;
            }
            @JavascriptInterface
            public void top() {
                Log.e("msg+++++++", "到了顶端");
                isBottom = false;
                isTop = true;
            }
        }, "java");
    }

    @Override
    protected void onCreateContextMenu(ContextMenu menu) {
        super.onCreateContextMenu(menu);
    }

    @Override
    public ActionMode startActionMode(ActionMode.Callback callback) {
        return super.startActionMode(callback);
    }
    //加载本地的pdf
    public void loadLocalPDF(String path) {
        loadUrl(PDFJS + "file://" + path);
    }

    //加载url的pdf
    public void loadOnlinePDF(String url) {
        loadUrl(PDFJS + url);
    }
}
